#!/usr/bin/env ruby
#
# Copyright 2009, Jeffrey Palm.
#

#
# Unit tests
#

require 'config'
require 'test/unit'
require 'resumep3'
require 'desktop_composed_common'
require 'retrieves_key_from_server'

class Tests < Test::Unit::TestCase

  FAKE_LIBRARY_XML = File.join 'test','test_library.xml'

  # ----------------------------------------------------------------------
  # Test
  # ----------------------------------------------------------------------

  class FakeIPhone
    attr_reader :unique_id
    def initialize
      @unique_id = 'lakjsdfljfa08adsf098asdf098asdflkjasdflkjasdf'
    end
  end
  
  def test_get_new_id
    r = RetrievesKeyFromSever.new Config.default,FakeIPhone.new
    shared_key = r.get_the_shared_key
    assert shared_key
  end
  
  def test_public_ip_address
    ip = public_ip_address
    assert ip =~ /\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
  end

  def test_private_ip_address
    ip = private_ip_address
    assert ip =~ /\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
  end

  def test_play_and_pause_track
    fake_setup
    track_id = find_test_track_id
    @r.play_track track_id
    info = @r.read_info 'testtesttest2'
    assert_equal PLAYING,info['state']
    sleep 1
    @r.pause_player
    info = @r.read_info 'testtesttest2'
    assert_equal PAUSED,info['state']
  end

  def test_arguments_print_help
    assert_equal @c.real_main(['-h',fake_shared_key]),RET_PRINTED_HELP
  end

  def test_arguments_print_help2
    assert_equal @c.real_main(['-help',fake_shared_key]),RET_PRINTED_HELP
  end

  def test_arguments_resume
    assert_equal @c.real_main(['-n','-r',fake_shared_key]),RET_PLAYED
  end

  def test_arguments_print_help2
    assert_equal @c.real_main(['-n','-resume',fake_shared_key]),RET_PLAYED
  end

  def test_arguments_resume
    assert_equal @c.real_main(['-c',fake_shared_key]),RET_JUST_CHECKED
  end

  def test_arguments_print_help2
    assert_equal @c.real_main(['-check',fake_shared_key]),RET_JUST_CHECKED
  end

  def test_arguments_paused
    assert_equal @c.real_main(['-n','-p',fake_shared_key]),RET_PAUSED
  end

  def test_arguments_paused2
    assert_equal @c.real_main(['-n','-pause',fake_shared_key]),RET_PAUSED
  end
  
  def test_track_id
    fake_setup
    info = create_fake_info
    track_id = @r.find_track_id info
    assert_equal '818',track_id
  end

  def test_read_info
    info = @r.read_info 'testtesttest'
  end
  
  def test_load_info
    info = @r.load_resume_info 'testtesttest'
    assert_equal 'Hoofprints In the Sand', info['track']
    assert_equal 'Sage Francis',           info['artist']
    assert_equal 'Human the Death Dance',  info['album']
  end

  def test_save_info
    @r.save_song create_fake_info('testtesttest2')
  end

  def test_save_and_load_info
    user = 'testtesttest2'
    @r.save_song create_fake_info(user)
    info = @r.load_resume_info user
    assert_equal 'Hoofprints In the Sand', info['track']
    assert_equal 'Sage Francis',           info['artist']
    assert_equal 'Human the Death Dance',  info['album']
  end

  def test_nasty_stuff_no_commas
    assert_equal 'abc', remove_nasty_stuff('a,b,c')
  end

  def test_nasty_stuff_no_periods
    assert_equal 'abc', remove_nasty_stuff('a.b.c')
  end

  def test_nasty_stuff_case
    assert_equal 'abc', remove_nasty_stuff('ABC')
  end

  def test_nasty_stuff_real1
    assert_equal 'im wide awakeits morning', remove_nasty_stuff('I\'m Wide Awake, It\'s Morning')
  end

  # From a bug appearing on line 334, where CGI.escaping a nil value
  # caused shit to go DOWN

  def test_save_song_with_a_nil_track
    @r.save_song create_fake_info_with_a_nil_track('testtesttest2')
  end

  def test_save_song_with_a_nil_artist
    @r.save_song create_fake_info_with_a_nil_artist('testtesttest2')
  end

  def test_save_song_with_a_nil_album
    @r.save_song create_fake_info_with_a_nil_album('testtesttest2')
  end

  def test_save_song_with_a_nil_user
    @r.save_song create_fake_info_with_a_nil_user('testtesttest2')
  end
  
  # ----------------------------------------------------------------------
  # Misc (and final)
  # ----------------------------------------------------------------------

  # Sends a simple message
  def send_message_on_socket
    streamSock = TCPSocket.new TEST_ADDR,TEST_PORT
    streamSock.send( "Hello\n" )  
    str = streamSock.recv( 100 )  
    print str  
    streamSock.close 
  end

  def find_test_track_id
    #
    # This sucks but it's the only way I can assure that we are using
    # the correct track id
    #
    xml = ITunes.new_instance.library
    lines = open(xml).read.split /\n/
    # <key>Track ID</key><integer>1141</integer>
    lines.each do |line|
      line.scan /<key>Track ID<\/key><integer>(\d+)<\/integer>/ do |res|
        return res[0]
      end
    end
    STDERR.puts 'Could\'nt find a track for ' + @r.itunes.to_s
    return nil
  end
    
  def setup
    @r = DesktopComposedCommon.new Config.default
    @c = CommandLine.new @r,ParseArgs.new
    if @warning
      puts '---------------------------------------------------'
      puts 'Make sure itunes is paused or stopped'
      if ENV['USER'] != 'jeff'
        puts 'This probably won\'t run without jeff as the user'
      end
      puts '---------------------------------------------------'
    end
    @warning = true
  end
    
  # Called when we *need* to use the mac instance
  def fake_setup
    # @r.itunes = ITunes::new_mac_instance
  end
  
  def teardown
    fake_setup
  end

  def create_fake_info(user=nil)
    return {
      'track'  => 'Hoofprints In the Sand',
      'artist' => 'Sage Francis',
      'album'  => 'Human the Death Dance',
      'user'   => user,
      'time'   => '8'
    }
  end

  def create_fake_info_with_a_nil_field(field,user=nil)
    info = create_fake_info user
    info[field] = nil
    info
  end

  def create_fake_info_with_a_nil_track(user=nil)
    create_fake_info_with_a_nil_field 'track',user
  end

  def create_fake_info_with_a_nil_artist(user=nil)
    create_fake_info_with_a_nil_field 'artist',user
  end

  def create_fake_info_with_a_nil_album(user=nil)
    create_fake_info_with_a_nil_field 'album',user
  end

  def create_fake_info_with_a_nil_user(user=nil)
    create_fake_info_with_a_nil_field 'user',user
  end

  def fake_shared_key
    'jkhdfksjhf9824asdfkjsdflkjsdflkjsdf'
  end

end

